<template>
  <div class="notice_container">
    <div class="header">
      <div class="scroll-text" ref="scrollText">
        <i class="el-icon-s-opportunity"></i> {{ text }}
        <i class="el-icon-s-opportunity"></i>
      </div>
    </div>
    <div class="banner">
      <div class="banner_header"><p>近期公告</p></div>
      <div class="banner_main"
           v-loading="loading"
           element-loading-text="拼命加载中"
           element-loading-spinner="el-icon-loading"
           element-loading-background="rgba(0, 0, 0, 0.8)">
        <div class="banner_main_item" v-for="item in noticeList" :key="item.noticeAdminId">
          <div class="banner_main_item_header">
            <p>{{ item.noticeTitle }} <span class="time">{{ item.createTime }}</span></p>
          </div>
          <div class="banner_main_item_main">
            <p>{{ item.noticeContent }}</p>
          </div>
        </div>
        <!-- Pagination Controls -->
        <div class="pagination">
          <!--TODO 实现用户公告翻页功能-->
          <!-- 分页查询区域 -->
          <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="queryInfo.pageNum"
              :page-sizes="[5, 10, 15]"
              :page-size="queryInfo.pageSize"
              layout="total, sizes, prev, pager, next"
              :total="this.total"
          >
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      text: "图书馆公告栏，记得及时查收公告噢！",
      noticeList: [],
      loading: true,
      queryInfo: {
        pageNum: 1,
        pageSize: 5,
      },
      total: 0, // 总公告条数
    };
  },
  methods: {
    handleSizeChange(val) {
      this.queryInfo.pageSize = val;
      // this.queryInfo.pageNum = 1; // 如果改变每页条数，页码回到第一页
      this.getRuleList();
    },
    handleCurrentChange(val) {
      this.queryInfo.pageNum = val;

      this.getRuleList();
    },
    async getRuleList() {
      this.loading = true;
      try {
        // const { data: res } = await this.$http.get('user/get_noticelist', { params: this.queryInfo });
        const {data:res} = await this.$http.post('user/get_noticelist',this.queryInfo)
        this.noticeList = [];
        if (res.status !== 200) {
          /*this.loading = false;
          return this.$message.error(res.msg);*/
          this.total = 0;
          this.loading = false;
          return this.$message.error(res.msg);
        }

        this.noticeList = res.data.records || [];  // 公告数据
        this.total = parseInt(res.data.total) || 0; // 假设返回数据中有total字段表示总条数
      } catch (error) {
        this.loading = false;
        this.$message.error("加载公告失败！");
      }
      this.loading = false;
    },
  },
  created(){
    this.getRuleList();
  },
  mounted() {
    const containerWidth = this.$refs.scrollText.offsetWidth;
    const textWidth = this.$refs.scrollText.scrollWidth;

    if (textWidth > containerWidth) {
      this.$refs.scrollText.style.animation = "scroll 60s linear infinite";
    }
    // this.getRuleList(this.currentPage);
    // 加载第一页的数据
    this.getRuleList();
  },
};
</script>

<style lang="less" scoped>
.notice_container {
  overflow: hidden;
  background: #f7f9fc;
  padding: 20px;
}

.header {
  width: 100%;
  height: 60px;
  background: linear-gradient(to right, #3e8fbb, #2c3e50);
  border-radius: 25px;
  color: white;
  text-align: center;
  line-height: 60px;
  font-size: 22px;
  font-family: 'Arial', sans-serif;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.scroll-text {
  white-space: nowrap;
  animation: scroll 12s linear infinite;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 1px;
}

@keyframes scroll {
  from {
    transform: translateX(100%);
  }
  to {
    transform: translateX(-100%);
  }
}

.banner {
  width: 100%;
  margin-top: 30px;
}

.banner_header {
  width: 100%;
  height: 80px;
  background-color: #f1f1f1;
  border-radius: 8px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);

  p {
    color: #333;
    font-size: 28px;
    text-align: center;
    line-height: 80px;
    font-family: 'Arial', sans-serif;
  }
}

.banner_main {
  width: 100%;
  height: 500px;
  display: flex;
  flex-direction: column;
  align-items: center;
  max-height: 520px;
  overflow-y: auto;
}

.banner_main_item {
  width: 80%;
  margin-top: 20px;
  border-radius: 12px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  background-color: #fff;
  transition: transform 0.3s ease;
  min-height: 160px;
}

.banner_main_item:hover {
  transform: translateY(-5px);
}

.banner_main_item_header {
  width: 100%;
  height: 60px;
  background-color: #4ab3f4;
  color: white;
  border-bottom: 2px solid #e1e1e1;
  font-size: 18px;
  text-align: center;
  line-height: 60px;
  font-weight: bold;
  display: flex;
  justify-content: space-between;
  padding: 0 20px;
}

.banner_main_item_header .time {
  font-size: 14px;
  color: rgba(255, 255, 255, 0.8);
}

.banner_main_item_main {
  width: 100%;
  height: 100px;
  padding: 15px;
  background-color: #fafafa;
  font-size: 14px;
  color: #555;
  text-align: justify;
  line-height: 1.6;
}

.banner_main_item_main p {
  margin: 0;
}

.pagination {
  width: 80%;
  margin-top: 10px;
  display: flex;
  justify-content: center;
}

@media (max-width: 768px) {
  .banner_main_item {
    width: 90%;
  }
}
</style>

